package com.sunyard.utils.manage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Description:日期转换工具类
 * @author:zhangbin
 * @date:2017年7月14日 上午11:28:11
 */
public class DateFormat {

    /**
     * 将日期转为yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateFormat1(Date date) {
        // 2011-09
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        return sdf.format(date);
    }

    /**
     * 将日期转为yyyyMMdd
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        // 2011-09
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    /**
     * 将日期转为HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String datetimeFormat(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取日期的年份
     *
     * @param date
     * @return
     */
    public static String getYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }

    /**
     * 获取日期的月份
     *
     * @param date
     * @return
     */
    public static String getMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return sdf.format(date);
    }

    /**
     * 获取日期的日
     *
     * @param date
     * @return
     */
    public static String getDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }

    /**
     * 将日期转为yyyy-MM-dd HH:mm:ss输出
     *
     * @param date
     * @return
     */
    public static String dateTimeFormat(Date date) {
        // 2011-09
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将日期转为yyyyMMddHHmmss输出
     *
     * @param date
     * @return
     */
    public static String dateTimeFormat1(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }

    public static String dateTimeFormatSSS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(date);
    }

    /**
     * 将yyyyMMddHHmmss转为yyyy-MM-dd HH:mm:ss格式输出
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String StringToDate1(String date) {
        String d = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            d = sdf.format(sdf1.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
        return d;
    }

    /**
     * 将yyyyMMdd转为yyyy-MM-dd 格式输出
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String StringToDate2(String date) {
        String d = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        try {
            d = sdf.format(sdf1.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
        return d;
    }

    /**
     * 计算2个时间相差几秒
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long comPareDate(String beginDate, String endDate) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long between = 0;
        Date begin;
        Date end;
        try {
            begin = dfs.parse(beginDate);
            end = dfs.parse(endDate);
            between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return between;
    }

    /**
     * 计算2个时间相差几秒
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long comPareDate(Date beginDate, Date endDate) {

        long between = (endDate.getTime() - beginDate.getTime()) / 1000;// 除以1000是为了转换成秒

        return between;
    }

    /**
     * @Title: getDate @Description: 根据指定格式以及时间构造Date @param @param
     * format @param @param date @param @return @param @throws
     * ParseException @return Date @throws
     */
    public static Date getDate(String format, String date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(date);
    }

    public static String getDateFormat1(String create_time) throws ParseException {

        String format = "";
        if (create_time != null && create_time != "NULL" && create_time != "") {
            if (isDate(create_time)) {
                format = create_time;
            } else {
                //转换日期格式(将Mon Jun 18 2018 00:00:00 GMT+0800 (中国标准时间) 转换成yyyy-MM-dd)
                create_time = create_time.replace("Z", " UTC");
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                Date d = sdf1.parse(create_time);//Mon Mar 06 00:00:00 CST 2017
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                format = sdf.format(d);//2017-03-06
            }
        }

        return format;
    }

    private static boolean isDate(String date) {
        /**
         * 判断日期格式和范围
         */
        String rexp = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(date);
        boolean dateType = mat.matches();
        return dateType;
    }
}
